package org.example.service.impl;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.example.constants.Constants;
import org.example.constants.GlobalExceptionCode;
import org.example.pojo.domain.Driver;
import org.example.mapper.DriverMapper;
import org.example.pojo.domain.DriverSetting;
import org.example.pojo.domain.DriverSummary;
import org.example.pojo.domain.DriverWallet;
import org.example.pojo.dto.OpenToCodeDto;
import org.example.pojo.properties.AppSettingProperties;
import org.example.pojo.util.Util;
import org.example.service.IDriverService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.service.IDriverSettingService;
import org.example.service.IDriverSummaryService;
import org.example.service.IDriverWalletService;
import org.example.sms.HttpClientUtil;
import org.example.utils.AssertUtil;
import org.example.utils.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

/**
 * <p>
 * 司机对象 服务实现类
 * </p>
 *
 * @author ?????
 * @since 2024-01-28
 */
@Service
public class DriverServiceImpl extends ServiceImpl<DriverMapper, Driver> implements IDriverService {

    @Autowired
    private IDriverSettingService driverSettingService;

    @Autowired
    private IDriverSummaryService driverSummaryService;

    @Autowired
    private IDriverWalletService driverWalletService;

    @Autowired
    private AppSettingProperties appSettingProperties;
//
//    @Autowired
//    private DriverSettingProperties driverSettingProperties;


    @Override
    public void wxRegister(String openIdCode) {
//        String urlTem ="https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code";
        String url = String.format(Constants.Url.WX_OPENIDCODE_TO_CODE_URL,appSettingProperties.getAppId(),appSettingProperties.getSecret(), openIdCode);
        String JsonResultStr =HttpUtil.get(url);
        OpenToCodeDto bean = JSONUtil.toBean(JsonResultStr, OpenToCodeDto.class);
        String openid = bean.getOpenid();
        //判断是否为空
        AssertUtil.isNotNull(openid, GlobalExceptionCode.PARAM_CODE_ERROR);
        //利用MyBatis-Plus中的LambdaQueryWrapper条件查询,再用查询到的openid和传进去的id比较
        Driver one = super.getOne(new LambdaQueryWrapper<Driver>().eq(Driver::getOpenId, openid));
        //判断结果是否为空
        AssertUtil.isNull(one,GlobalExceptionCode.PARAM_PHONE_EXIST);
        Long l = saveDriver(openid);
        //初始化
        initDriverSetting(l);
        initDriverSummary(l);
        initDriverWallet(l);




    }
    private void initDriverWallet(Long id) {
        DriverWallet driverWallet = new DriverWallet();
        driverWallet.setId(id);
        driverWallet.setCreateTime(new Date());
        driverWallet.setAmount(BigDecimal.ZERO);
        // 随机生成一个密码，给用户发短信 @TODO 明日自研  研究阿里云发送短信
        String pass = Util.getRandomString(6);
        driverWallet.setPassword(pass);
        String string = id.toString();
        // 发送短信
        senMessage(pass);
        driverWalletService.save(driverWallet);
    }

    private void initDriverSummary(Long id) {
        DriverSummary driverSummary = new DriverSummary();
        driverSummary.setId(id);
        driverSummary.setLevel(Constants.RealAuth.VERIFYING);
        driverSummary.setTotalOrders(0);
        driverSummary.setWeekOrders(0);
        driverSummary.setGoodComments(0);
        driverSummary.setAppeal(0);
        driverSummary.setTotalComplaint(0);
        driverSummary.setTodayTotalCancel(0);
        driverSummary.setDriveDuration(0);
        driverSummary.setTradeOrders(0);
        driverSummary.setTodayTradeOrders(0);
        driverSummary.setTodayComplaint(0);
        driverSummary.setTodayCancel(0);
        driverSummary.setTodayIncome(BigDecimal.ZERO);
        driverSummary.setCreateTime(new Date());
        driverSummaryService.save(driverSummary);
    }

    private void initDriverSetting(Long id) {
        DriverSetting driverSetting = new DriverSetting();
        driverSetting.setId(id);
        driverSetting.setAutoAccept(false);
        driverSetting.setOrientation(false);
        driverSetting.setListenService(false);
//        driverSetting.setOrderDistance(driverSettingProperties.getOrderDistance());
//        driverSetting.setRangeDistance(driverSettingProperties.getRangeDistance());
        driverSetting.setCreateTime(new Date());
        driverSettingService.save(driverSetting);
    }

    private Long saveDriver(String openid) {
        Driver driver = new Driver();
        driver.setPhone("");
        driver.setCreateTime(new Date());
        driver.setOpenId(openid);
        super.save(driver);
        return driver.getId();
    }

    private void senMessage(String pass){
        String uid ="斯比德s";
        String key ="3194D9BA94FB5C7175280A348CE975D7";
        HttpClientUtil client = HttpClientUtil.getInstance();
        int resultGbk = client.sendMsgGbk(uid, key, "您的密码是"+pass, "18708423092");
        if (resultGbk > 0) {
            System.out.println("GBK成功发送条数==" + resultGbk);
        } else {
            System.out.println(client.getErrorMsg(resultGbk));
        }

    }
}
